﻿using System.Data.Entity;
using Core.Config;
using Core.Log;
using FrameWork.Context;
using MyDb.DoMain.Entity.DbEntity;

namespace MyDb.DoMain.DbContext
{
    public class MyDbContext: DbContextBase
    {
        public MyDbContext() : base(CachedConfigContext.Current.DaoConfig.MyDb, new LogDbContext())
        {
        }

        #region 实体关系配置
        
        /// <summary>
        /// 配置数据表之间的关系
        /// </summary>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<MyDbContext>(null);

            //菜单与功能关联关系  一对多
            modelBuilder.Entity<T_Permission>().HasRequired(p => p.Module).WithMany(m => m.Permissions);

            //用户关联角色关系 
            modelBuilder.Entity<T_UserRole>().HasRequired(t => t.User).WithMany(m => m.UserRoles);

            //角色菜单关联关系  一对多
            modelBuilder.Entity<T_RoleModule>().HasRequired(rm => rm.Role);
        }

        #endregion


        #region 数据集

        /// <summary>
        /// 用户集
        /// </summary>
        public DbSet<T_User> Users { set; get; }

        /// <summary>
        /// 角色集
        /// </summary>
        public DbSet<T_Role> Roles { set; get; }

        /// <summary>
        /// 角色菜单集
        /// </summary>
        public DbSet<T_RoleModule> RoleModules { set; get; }

        /// <summary>
        /// 异常数据集
        /// </summary>
        public DbSet<Log4Net> Log4Nets { set; get; }

        /// <summary>
        /// 登录日志数据集
        /// </summary>
        public DbSet<T_LoginLog> LoginLogs { set; get; }

        /// <summary>
        /// 菜单集
        /// </summary>
        public DbSet<T_Module> Modules { set; get; }

        /// <summary>
        /// 功能集
        /// </summary>
        public DbSet<T_Permission> Permissions { set; get; }

        #endregion
    }
}
